package org.example;

public class Test3 {
    //leetcode 平衡二叉树 https://leetcode.cn/problems/balanced-binary-tree/description/
    public boolean isBalanced(TreeNode root) {
        return getDeepth(root) != -1;
    }

    public int getDeepth(TreeNode root) {
        if (root == null) return 0;
        int left = getDeepth(root.left);
        if (left == -1) return -1;
        int right = getDeepth(root.right);
        if (right == -1) return -1;
        return  Math.abs(left - right) > 1 ? -1 : Math.max(left,right) + 1;
    }
}
